<%--
  Created by IntelliJ IDEA.
  User: liubin
  Date: 2023/2/16
  Time: 14:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<style>
    .login_info{
        display: flex;
    }
  .login_page{
      margin-top: 10%;
    width: 30%;
    float: left;
  }
  .login_img{
      /*background-image: url("");*/
  }
</style>
<script src="https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/css/layui.min.css" integrity="sha512-omRxviAbZbsRLmYjGYaOjLuafC5Jw17PYyg1eH4XaT5vWx+cOng6t+bq9VyjZBWrUuduYgYuIuD2d3MOz7S2dA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/layui/2.7.6/layui.js" integrity="sha512-mIKH3M2bRlIyhG4tBEbJ8dn8t8JFlNJU2NXlJePgpQ72CK4jAYsZyCGFcASRGtPBbcAQhz67KTkA1Jw6Kizk9g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<body>
<div class="login_info">
    <div class="login_page layui-form-pane">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input id="name" type="text" name="title" required  lay-verify="required" placeholder="请输入账户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码框</label>
                <div class="layui-input-inline">
                    <input id="pwd" type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
                <%--      <div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input id="reCode" type="text" name="password" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <img src="" id="reCodeImg" onclick="initReqCode()">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">记住密码</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="switch" lay-skin="switch" id="checkBoxID">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo" type="button" onclick="toLogin()">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <div class="login_img"></div>
</div>
</body>
<script>
  var basePath = this.location.href
  basePath=basePath.substring(0,basePath.lastIndexOf("/"))
  console.log(basePath);
  $(function() {
    initForm();
    initReqCode()
  })
  function toMain(){
      $.ajax({
          url:basePath+"/student/toMain",
          async:true
      })
  }
  function toLogin(){
    var param = {
        username:$("#name").val(),
        pwd:$("#pwd").val(),
        reqCode:$("#reCode").val()
    };

    $.ajax({
      type : "POST",
      url:basePath+"/user/toLogin",
        async:false,
      data:JSON.stringify(param),
      contentType : "application/json;charset=UTF-8",
      success : function(data) {
        if (data.code==0) {
          if ($("#checkBoxID").is(":checked")){
            $.cookie("userName",$("#name").val(),{expires :7,path:"/"});
            $.cookie("pwd",$("#pwd").val(),{expires :7,path:"/"})
          }
          var url="/student/toMain"
         location.href=basePath+url
        }else{
            layer.open({
                title:'登录提示',
                content:data.msg,
                type:0,
                // time:10000
            });
        }
      },
      error:function () {
        console.log("发生错误")
      }
    });
  }
  function initReqCode(){
    $.ajax({
      url:  basePath+"/user/reqCode",
      type: "POST",
      async:false,
      success : function(data) {
        $("#reCodeImg").attr("src",data)
      },
      error : function(data) {
        console.log("请求错误")
      }
    })
  }
  function initForm() {
    let userName = $.cookie('userName');
    let pwd = $.cookie('pwd');
    if(userName!=undefined&&userName!='undefined'&&userName!=null&&userName!=''){
      $("#name").val(userName);
    }
    if(pwd!=undefined&&pwd!='undefined'&&pwd!=null&&pwd!=''){
      $("#pwd").val(pwd);
    }
  }
</script>
</html>
